package gitapp.oschina.net.my_osc.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kymjs.core.bitmap.client.BitmapCore;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import gitapp.oschina.net.my_osc.AppContext;
import gitapp.oschina.net.my_osc.R;
import gitapp.oschina.net.my_osc.bean.User;
import gitapp.oschina.net.my_osc.common.BroadcastController;
import gitapp.oschina.net.my_osc.common.StringUtils;
import gitapp.oschina.net.my_osc.interfaces.DrawerMenuCallBack;
import gitapp.oschina.net.my_osc.widget.CircleImageView;

/**
 * 导航栏菜单
 */
public class DrawerNavigationMenu extends Fragment implements View.OnClickListener {


    @InjectView(R.id.iv_portrait)
    CircleImageView ivPortrait;
    @InjectView(R.id.tv_name)
    TextView tvName;
    @InjectView(R.id.menu_user_info_layout)
    LinearLayout menuUserInfoLayout;
    @InjectView(R.id.menu_user_info_login_tips_layout)
    LinearLayout menuUserInfoLoginTipsLayout;
    @InjectView(R.id.menu_item_explore)
    LinearLayout menuItemExplore;
    @InjectView(R.id.menu_item_myself)
    LinearLayout menuItemMyself;
    @InjectView(R.id.menu_item_language)
    LinearLayout menuItemLanguage;
    @InjectView(R.id.menu_item_shake)
    LinearLayout menuItemShake;
    @InjectView(R.id.menu_item_scan)
    LinearLayout menuItemScan;
    @InjectView(R.id.menu_item_setting)
    LinearLayout menuItemSetting;

    public static DrawerNavigationMenu newInstance() {
        return new DrawerNavigationMenu();
    }

    private View mSavedView;
    private DrawerMenuCallBack mCallBack;
    private AppContext appContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof DrawerMenuCallBack) {
            mCallBack = (DrawerMenuCallBack) context;
        }
        BroadcastController.registerUserChangeReceiver(mUserChangeReceiver, context);
    }

    private BroadcastReceiver mUserChangeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            setupUserView();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.activity_main_drawer_menu, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        appContext = (AppContext) getActivity().getApplication();
        setupUserView();
    }

    private void initView(View view){
        highlightSelectedItem(menuItemExplore);
    }

    public void highlightExplore() {
        highlightSelectedItem(menuItemExplore);
    }

    private void highlightSelectedItem(View v){
        setSelected(null, false);
        setSelected(v, true);
    }

    private void setSelected(View v, boolean selected){
        View view;
        if (v == null && mSavedView == null){
            return;
        }
        if (v != null){
            mSavedView = v;
            view = mSavedView;
        }else {
            view = mSavedView;
        }
        if (selected){
            ViewCompat.setHasTransientState(view, true);
            view.setBackgroundColor(getResources()
                    .getColor(R.color.menu_layout_item_pressed_color));
        }else {
            ViewCompat.setHasTransientState(view, false);
            view.setBackgroundResource(R.drawable.menu_layout_item_selector);
        }
    }

    private void setupUserView(){
        //判断是否已经登录，如果已登录则显示用户的头像与信息
        if (!appContext.isLogin()){
            ivPortrait.setImageResource(R.mipmap.mini_avatar);
            tvName.setText("");
            menuUserInfoLayout.setVisibility(View.GONE);
            menuUserInfoLoginTipsLayout.setVisibility(View.VISIBLE);
            return;
        }
        menuUserInfoLayout.setVisibility(View.VISIBLE);
        menuUserInfoLoginTipsLayout.setVisibility(View.GONE);
        tvName.setText("");

        User user = appContext.getLoginInfo();

        //加载用户头像
        String portrait = user.getNew_portrait() == null
                || user.getNew_portrait().equals(null)?
                "":user.getNew_portrait();
        if (portrait.endsWith("portrait.gif") ||
                StringUtils.isEmtry(portrait)){
            ivPortrait.setImageResource(R.mipmap.mini_avatar);
        }else {
            new BitmapCore.Builder().url(portrait)
                    .view(ivPortrait).doTask();
        }
        tvName.setText(user.getName());
    }

    @Override
    public void onDetach() {
        super.onDetach();
        BroadcastController.unregisterReceiver(getActivity(),
                mUserChangeReceiver);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    @OnClick({R.id.menu_user_layout, R.id.menu_item_explore, R.id.menu_item_myself,
            R.id.menu_item_language, R.id.menu_item_shake,
            R.id.menu_item_scan, R.id.menu_item_setting})
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.menu_user_layout:
                onClickLogin();
                break;
            case R.id.menu_item_explore:
                onClickExplore();
                highlightSelectedItem(v);
                break;
            case R.id.menu_item_myself:
                onClickMySelf();
                if (appContext.isLogin()){
                    highlightSelectedItem(v);
                }
                break;
            case R.id.menu_item_language:
                onClickLanguage();
                break;
            case R.id.menu_item_shake:
                onClickShake();
                break;
            case R.id.menu_item_scan:
                onClickScan();
                break;
            case R.id.menu_item_setting:
                onClickSetting();
                break;
            default:
                break;
        }
    }

    private void onClickLogin(){
        if (mCallBack != null){
            mCallBack.onClickLogin();
        }
    }

    private void onClickSetting(){
        if (mCallBack != null){
            mCallBack.onClickSetting();
        }
    }

    private void onClickExplore() {
        if (mCallBack != null) {
            mCallBack.onClickExplore();
        }
    }

    private void onClickMySelf() {
        if (mCallBack != null) {
            mCallBack.onClickMySelf();
        }
    }

    private void onClickLanguage() {
        if (mCallBack != null) {
            mCallBack.onClickLanguage();
        }
    }

    private void onClickShake() {
        if (mCallBack != null) {
            mCallBack.onClickShake();
        }
    }

    private void onClickScan() {
        if (mCallBack != null) {
            mCallBack.onClickScan();
        }
    }
}
